package com.haozi.ehub.core.handler;

import com.alibaba.fastjson.JSONObject;
import com.haozi.ehub.core.enums.HttpEunm;
import com.haozi.ehub.core.util.CodeUtils;
import com.haozi.ehub.core.util.ResponseUtils;
import com.haozi.ehub.core.util.ReturnDataUtils;
import com.haozi.ehub.entity.entities.ReturnData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 登出
 * @author ：terry
 * @date ：Created in 2020/6/23 14:07
 * @description：TODO
 * @version: 1.0
 */
@Slf4j
@SuppressWarnings(value = "all")
public class SecurityLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        log.info("Execute pacakage core handler securityLogoutSuccessHandler");
        String msg = HttpEunm.LOGIN_OUT.getMsg();
        ReturnData returnData = ReturnDataUtils.returnDataFail(HttpStatus.OK,msg);
        String json = JSONObject.toJSONString(returnData);

        //解决跨域
        ResponseUtils.setHttpSerlvetResponse(httpServletResponse);

        //输出返回结果的JSON
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(json);
        writer.flush();
    }
}
